package com.hzcx.app.simplechat.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.event.FinishEvent;
import com.hzcx.app.simplechat.ui.login.LoginActivity;
import com.hzcx.app.simplechat.ui.publicui.PublicConfirmDialog;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.cache.CacheUtils;
import com.hzcx.app.simplechat.util.token.TokenUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置界面
 */
public class SettingActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_private)
    TextView tvPrivate;
    @BindView(R.id.tv_group_message)
    TextView tvGroupMessage;
    @BindView(R.id.tv_auto_send_msg)
    TextView tvAutoSendMsg;
    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.tv_clear_cache)
    TextView tvClearCache;
    @BindView(R.id.tv_about)
    TextView tvAbout;
    @BindView(R.id.tv_login_out)
    TextView tvLoginOut;
    @BindView(R.id.tv_cache_size)
    TextView tvCacheSize;
    @BindView(R.id.tv_version_name)
    TextView tvVersionName;

    @Override
    public int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("设置");
        tvAbout.setText("关于" + getResources().getString(R.string.app_name));

        try {
            tvCacheSize.setText(CacheUtils.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }

        tvVersionName.setText(AppHelper.getVersionName(this));
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_clear_cache, R.id.tv_account, R.id.tv_notice, R.id.tv_private, R.id.tv_auto_send_msg, R.id.tv_about, R.id.tv_login_out,
            R.id.tv_group_message, R.id.tv_batch_clear, R.id.tv_vip_setting, R.id.tv_chat_manager})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_clear_cache: // 清理缓存
                clearCache();
                break;

            case R.id.tv_account: // 账号与安全
                startActivity(new Intent(this, AccountActivity.class));
                break;

            case R.id.tv_notice: // 通知设置
                startActivity(new Intent(this, SettingNoticeActivity.class));
                break;

            case R.id.tv_private: // 隐私设置
                startActivity(new Intent(this, SettingPrivateActivity.class));
                break;

            case R.id.tv_auto_send_msg: // 自动打招呼
                startActivity(new Intent(this, SettingAutoSendMsgActivity.class));
                break;

            case R.id.tv_about: // 关于XX
                startActivity(new Intent(this, AboutActivity.class));
                break;

            case R.id.tv_login_out: // 退出登录
                showConfirmDialog("提示", "退出后不会删除任何历史数据，下次登录依然可以使用本账号。", null, () -> loginOut());
                break;

            case R.id.tv_group_message: // 群发助手
                startActivity(new Intent(this, GroupMessageListActivity.class));
                break;

            case R.id.tv_batch_clear: // 批量清除
                if (UserInfoUtil.getUserIsVip()) {
                    startActivity(new Intent(this, BatchClearActivity.class));
                } else {
                    showConfirmDialog("提示", "此功能为VIP特权功能", "开通会员", () ->
                            startActivity(new Intent(SettingActivity.this, VipActivity.class)));
                }
                break;

            case R.id.tv_vip_setting: // 会员设置
                startActivity(new Intent(this, VipSettingActivity.class));
                break;

            case R.id.tv_chat_manager: // 聊天管理
                startActivity(new Intent(this, ChatSettingManagerActivity.class));
                break;
        }
    }

    /**
     * 清理缓存
     */
    private void clearCache() {
        try {
            showConfirmDialog("提示", "缓存大小为" + CacheUtils.getTotalCacheSize(this) + ",确定要清理缓存吗？", null, new PublicConfirmDialog.OnPublicConfirmClickListener() {
                @Override
                public void publicConfirm() {
                    CacheUtils.clearAllCache(SettingActivity.this);
                    tvCacheSize.setText("0M");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            showError("缓存数据异常，请稍后重试");
        }
    }

    /**
     * 退出登录
     */
    private void loginOut() {
        showLoading();
        new Thread(() -> {
            TokenUtils.logout(SettingActivity.this);
            EventBus.getDefault().post(new FinishEvent());
            startActivity(new Intent(SettingActivity.this, LoginActivity.class));
        }).start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
